#include "./base_win.h"
#include "../utils/util.h"
#include <QDebug>
#include <QHBoxLayout>


static constexpr int MARGIN = 5;

BaseWin::BaseWin(QWidget *parent)
    : QWidget(parent), m_centra(new QWidget(this)) {

    QHBoxLayout *layout = new QHBoxLayout();
    layout->setMargin(MARGIN);
    layout->addWidget(m_centra);
    setLayout(layout);

    set_window_custom(this);
    set_window_card(m_centra, MARGIN * 2);
}

void BaseWin::setup() {
    resize(m_centra->width() + MARGIN * 2, m_centra->height() + MARGIN * 2);
    // run_scale_anim(this, 200, true);
}

void BaseWin::mousePressEvent(QMouseEvent *event) {
    if (event->button() == Qt::LeftButton) {
        m_is_pressed = true;
        m_drag_position = event->globalPos() - frameGeometry().topLeft();
        event->accept();
        return;
    }
    return QWidget::mousePressEvent(event);
}

void BaseWin::mouseMoveEvent(QMouseEvent *event) {
    if (m_is_pressed && (event->buttons() & Qt::LeftButton)) {
        move(event->globalPos() - m_drag_position);
        event->accept();
        return;
    }
    return QWidget::mouseMoveEvent(event);
}

void BaseWin::mouseReleaseEvent(QMouseEvent *event) {
    if (event->button() == Qt::LeftButton) {
        m_is_pressed = false;
    }
}

void BaseWin::closeEvent(QCloseEvent *event) {
    QWidget::closeEvent(event);
    //    if (m_close_anim_flag) {
    //        QWidget::closeEvent(event);
    //        return;
    //    }
    //    event->ignore();
    //    m_close_anim_flag = true;
    //    auto anim = run_scale_anim_close(this, 150);
    //    connect(anim, &QPropertyAnimation::finished, this, [this] () {
    //        close();
    //    });
}
